/*
******************
* author: 马丰彦
* date: 2019-05-08
* function: 用户登录登出、存取cookie、以及获取权限
******************
*/

import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  plat: [],
  roles: [],
  assignment: [],
  aaaSystem: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PLAT: (state, plat) => {
    state.plat = plat
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ASSIGNMENT: (state, assignment) => {
    state.assignment = assignment
  },
  SET_AAASYSTEM: (state, aaaSystem) => {
    state.aaaSystem = aaaSystem
  }
}

const actions = {
  // 用户登录 action
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        // 存取token
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 得到用户权限
  getInfo({ commit, state }, baseUrl) {
    return new Promise((resolve, reject) => {
      const url = baseUrl + 'system/queryUserPower'
      console.log(url)
      getInfo(state.token, url).then(response => {
        const { sysList } = response.data
        const { funList } = response.data
        // SET_PLAT()
        // roles must be a non-empty array
        var roles = []
        var newArr
        var newPower
        var newAss
        var assignment = []
        // var platForm = []
        var left
        if (Array.isArray(funList) && funList.length > 0) {
          newPower = funList.filter(item => {
            if (item.powerType === 'plat' && item.powerCode.indexOf('integral') !== -1) {
              return item
            }
          })
        }
        if (Array.isArray(sysList) && sysList.length > 0) {
          newAss = sysList.filter(item => {
            if (item.powerType === 'button' && item.powerCode.indexOf('integral') !== -1) {
              return item
            }
          })
          left = sysList.filter(item => {
            if (item.powerType === 'system') {
              return item
            }
          })
          newArr = sysList.filter(item => {
            if (item.powerType === 'menu' && item.powerCode.indexOf('integral') !== -1) {
              return item
            }
          })
          newArr.forEach(item => {
            if (item.powerType === 'menu') {
              roles.push(item.powerCode)
            }
          })
          newAss.forEach(item => {
            if (item.powerType === 'button') {
              assignment.push(item.powerCode)
            }
          })
        }
        // console.log(roles)
        if (!newArr || newArr.length <= 0) {
          reject('权限列表必须为数组！')
        }
        const rolesObj = {
          roles: roles
        }
        commit('SET_AAASYSTEM', left)
        commit('SET_ROLES', roles)
        commit('SET_TOKEN', getToken())
        commit('SET_PLAT', newPower)
        commit('SET_ASSIGNMENT', assignment)
        resolve(rolesObj)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

